/*
* LayerBox浮层效果
* @author 陈桥
* @version 1.0
*/
WOYO.add('widget.LayerBox',function(W) {
	var defaultConfig = {
		opacity:0.5,
		backcolor:'#ccc',
		zIndex:9,
		isFrame:true,
		text:'alert 弹出浮层',
		time:3000,
		struc:null
	}, 
	defaultMaskConfig = {
		display: 'none',
		position:'absolute',
		backgroundColor:'#ccc',//遮罩层背景色
		zIndex:99999,//遮罩层层级
		top:'0px',
		left:'0px',
		height : '768px',
		width  : '1024px',
		backgroundImage:'url(about:blank;)',//IE兼容设置
		opacity:0.5,//透明度设置
		filter:'alpha(opacity=50)',
		mozOpacity:0.5
	};
	//setOption(el, {id: 'asins', class:'bb'})
	function setOption(el, obj){
		W.each(obj, function(val, key){
			el[key] = val;
		});
	};

	function ClassObj(eventType, config) {
		var self = this;
		if (! (self instanceof ClassObj)) {
			return new ClassObj(eventType, W.merge(defaultConfig, config));
		}
		var config = self.config = config;
		//初始值
		//检查是否为IE6
		var ie = window.ActiveXObject && !window.XMLHttpRequest;
		//计算网页最大值
		/*var config_val ={
				ch : Math.max(document.documentElement.clientHeight,document.documentElement.scrollHeight),
				cw : Math.max(document.documentElement.clientWidth,document.documentElement.scrollWidth)
			};*/
		//生成唯一标识
		var uuid=Date.parse(new Date())+Math.floor(Math.random() * 100000);
		//获取document对象
		var getDOM = function(i){
			this.i = i;
			switch(i.substr(0,1)){
				case '#':return getID();break;
				case '.':return getClass();break;
				default:return getClass();break;
			}
			function getID(){
				var _tag = this.i.split('#').length > 1 ? this.i.split('#') : [null,this.i];
				return document.getElementById(_tag[1]);
			}
			function getClass(){
				var _tag = this.i.split('.').length > 1 ? this.i.split('.') : [null,this.i];
				var ele=[],all=document.getElementsByTagName(_tag[0]||"*");
				for(var i=0;i<all.length;i++){
					if(all[i].className==_tag[1]){
					ele[ele.length]=all[i];
					}
				}
				return ele;
			}
		};
		//点击事件
		var click = function(doc,fn){
			doc.onclick = fn;
		};
		//窗口事件
		resizeList=[];
		function resize(fn){
			resizeList.push(fn);
			window.onresize=function(){
				for(var i=0;i<resizeList.length;i++){
					resizeList[i].call(window);
				};
			};
		};
		//滚动条事件
		scrollList = [];
		function scroll(fn){
			scrollList.push(fn);
			window.onscroll=function(){
				for(var i=0;i<scrollList.length;i++){
					scrollList[i].call(window);
				};
			};
		};
		//遮罩 初始化
		function Mask() {
			var _lay_MASK = getDOM('div.layerbox_mask'+uuid);
			if(!_lay_MASK.length){
				//创建div 元素
				this.el = document.body.appendChild(document.createElement('DIV'));
				//遮罩层样式设置
				var page ={
					backgroundColor :  config.backcolor,
					zIndex :  config.zIndex,
					height :  Math.max(document.documentElement.clientHeight,document.documentElement.scrollHeight) + 'px',
					width  :  Math.max(document.documentElement.clientWidth,document.documentElement.scrollWidth) + 'px',
					opacity:  config.opacity,//firefox3.5 + 
					filter :  "alpha(opacity="+(config.opacity * 100) +")",//IE6 + 
					mozOpacity: config.opacity//firefox3.5 -
				};
				setOption(this.el.style, W.merge(defaultMaskConfig, page));//设置样式
				var _thiselstyle =this.el.style;
				//游览器状态发生变化时执行
				resize(function(){
					_thiselstyle.height =  Math.max(document.documentElement.clientHeight,document.documentElement.scrollHeight) + 'px';
					_thiselstyle.width =  Math.max(document.documentElement.clientWidth,document.documentElement.scrollWidth) + 'px';
				});

				//遮罩层name
				this.el.className = 'layerbox_mask'+uuid;
				//判断是否IE6.0
				if (ie) {
					var css = 'width:100%;height:100%;display:block;filter:Alpha(opacity=0);';
					this.el.innerHTML = '<iframe frameborder="no" style="' + css + '"></iframe><div style="position:absolute;z-index:2;left:0;top:0;' + css + '"></div>';
				};
			}else{
				this.el = _lay_MASK[0];
			}
		};
		Mask.prototype = {
			show: function(zIndex) {
				this.el.style.display = 'block';
			},
			hide: function() {
				this.el.style.display = 'none';
			}
		};
/*
		//遮罩层 使用
		if(config.isFrame){ 
			var _mask = new Mask();
		};
		var mask_show = function(time){
			_mask.show();
			setTimeout(this.hide,time);
		};
		var mask_hide = function(){
			_mask.hide();
		};
*/
		//浮层定位
		function Location(){
			
		//居中定位
		this.center = function(obj){
				obj.style.display = 'block';
				obj.style.top = (document.documentElement.clientHeight - obj.offsetHeight) / 2 + 'px';
				obj.style.left = (document.documentElement.clientWidth - obj.offsetWidth) / 2 + 'px';
				obj.style.position = 'fixed';
				obj.style.display = 'none';
				obj.style.zIndex = config.zIndex + 1;
				//游览器状态发生变化时执行
				resize(function(){
					obj.al.style.top = (document.documentElement.clientHeight - obj.offsetHeight) / 2 + 'px';
					obj.al.style.left = (document.documentElement.clientWidth - obj.offsetWidth) / 2 + 'px';
				});
				//ie6兼容
				if(ie){
					obj.style.position = 'absolute';
					var _top = parseInt(obj.style.top);
					scroll(function(){
						obj.style.top = _top + document.documentElement.scrollTop + 'px';
					});
				}
			}
		}

		//点击弹出层
		function Dialog(){
				this.al = document.body.appendChild(document.createElement('DIV'));
				this.al.className="layerbox_dialog_alert";
				//遮罩层样式设置
				var page ={
					left : '700px',
					top : '400px',
					position : 'absolute',
					width  : 'auto',
					height : 'auto',
					zIndex : config.zIndex + 1,
					display : 'none',
					backgroundColor : '#FFF',
					fontSize: '12px',
					border:'2px solid #d8d8d8',
					textAlign:'center',
					float:'left'
				};
				setOption(this.al.style, page);//设置样式
				this.al.innerHTML = '';
		};
		Dialog.prototype = {
			show: function(text) {
				this.al.style.display = 'block';
			},
			hide: function() {
				this.al.style.display = 'none';
			}
		};
		var style = function(){
			this.dialog=null;
			this._mk = null;
		};
		//遮罩层
		style.mask = function(){
			var _mask = null;
			if(config.isFrame){ 
				_mask = new Mask();
			};
			this.mask_show = function(time){
				_mask&&_mask.show();
			}
			this.mask_hide = function(){
				_mask&&_mask.hide();
			};
		};
		//弹出层效果
		var st = new style();
		style.alert = function(text,time){
			if(!st.dialog){
				st.dialog = new Dialog();//初始弹出层
				st._mk = new style.mask();//初始化遮罩层
				st.dialog.al.innerHTML = '<p style="margin:20px 50px 5px 50px">'+(text || config.text)+'</p><button style="width:auto;height:auto;margin:10px 10px 10px 10px">关闭</button>';//默认弹出层结构
				var loc = new Location();
				loc.center(st.dialog.al);
			}
			st._mk.mask_show();//显示遮罩
			st.dialog.show();//显示弹出
			var stout = setTimeout(hide,time || config.time);//定时隐藏弹出层
			click(st.dialog.al.childNodes[1],function(){//关闭按钮绑定关闭事件
				clearInterval(stout);
				hide();
			});
			function hide(){
				st._mk.mask_hide();
				st.dialog.hide();
			};
		};

		//弹出结构
		style.struc = function(time,text){
			if(!st.dialog){
	
				st._mk = new style.mask();//初始化遮罩层

				st.dialog = getDOM('div.pop_box')[0];

				//console.log(st.dialog.firstChild);

				var loc = new Location();
				loc.center(st.dialog);
			}
			st._mk.mask_show();//显示遮罩
			st.dialog.style.display='block';//显示弹出
			var stout = setTimeout(hide,time || config.time);//定时隐藏弹出层

			//console.log(st.dialog.firstChild.nextSibling);
			/*
			while (!st.dialog.firstChild.tagName) { 
				console.log(st.dialog.firstChild.nextSibling);
			}*/
			//console.log(st.dialog.firstChild);

			click(st.dialog.childNodes[1],function(){//关闭按钮绑定关闭事件
				clearInterval(stout);
				hide();
			});
			function hide(){
				st._mk.mask_hide();
				st.dialog.style.display='none';
			};
		};



		//点击弹出层
		function mask_show(time){
			_mask.show();
		}
		function mask_hide(){
			_mask.hide();
		};

		this.alert = function(text,time){
			switch (eventType) {
				case 'alert':style.alert(text,time);break;
				case 'struc':style.struc();break;
				default:style.alert(time);break;
			};
		}
	};
	W.augment(ClassObj, {
		close: function() {},
		alert: function(text,time) {
			this.alert(text,time);
		},
		hide:function(){
			this.hide();
		},
		show:function(){
			this.alert();
		}
	});
	return ClassObj;
});
